package com.work.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.common.exp.WorkException;
import com.work.entity.UserLogin;
import com.work.mapper.UserMapper;
import com.work.service.EnterpriseAssociationService;
import com.work.service.UserLoginService;
import com.work.vo.UserRegisterVO;
import com.work.common.enums.UserLoginType;
import com.work.common.util.AssertUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;


@Service
@RequiredArgsConstructor
public class UserLoginServiceImpl extends ServiceImpl<UserMapper, UserLogin> implements UserLoginService {
    private final PasswordEncoder passwordEncoder;
    private final EnterpriseAssociationService enterpriseAssociationService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserLogin userLogin = getOne(
                Wrappers.lambdaQuery(UserLogin.class)
                        .eq(UserLogin::getUsername, username)
        );
        AssertUtil.nonNull(userLogin, UserLoginType.ACCOUNT_NOT_EXIST.getMsg());
        final UserLoginType accountDisabled = UserLoginType.ACCOUNT_DISABLED;
//        检验账户是否禁用
        AssertUtil.isFalse(
                accountDisabled.getCode() == userLogin.getStatus(),
                accountDisabled.getMsg());

        return userLogin;
    }

    @Override
    public boolean register(UserRegisterVO userRegisterVO) {
        CopyOptions copyOptions = new CopyOptions();
        copyOptions.setIgnoreProperties("code");
        final UserLogin bean = BeanUtil.toBean(userRegisterVO, UserLogin.class, copyOptions);
        bean.setStatus(0);
        bean.setDeleted(0);
        bean.setPassword(passwordEncoder.encode(userRegisterVO.getPassword()));
        return save(bean);
    }

    @Override
    public boolean userExist(String username) {
        return exists(Wrappers.lambdaQuery(UserLogin.class).eq(UserLogin::getUsername, username));
    }

    @Override
    public boolean userExistById(Long userId) {
        final boolean exists = exists(Wrappers.lambdaQuery(UserLogin.class).eq(UserLogin::getId, userId));
        if (exists) {
            return true;
        }
        throw new WorkException(10032, "用户不存在");
    }

    @Override
    public List<UserLogin> getUserListByIds(Long enterpriseId) {
        List<Long> users = enterpriseAssociationService.getUserByEnterpriseId(enterpriseId);
        if (CollectionUtils.isEmpty(users)) {
            return Collections.emptyList();
        }
        return Optional.ofNullable(
                super.listByIds(users)
        ).orElseGet(Collections::emptyList);
    }
}
